﻿using Bentley.GeometryNET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheccCoverplateCulvertAssemble.Operation.section
{
    internal class PrefabricateSlabCulvertSubsection//预制
    {
        /// <summary>
        /// 计算盖板涵分段数据
        /// </summary>
        /// <param name="slabCulvertLength">涵长</param>
        /// <param name="leftCustomizationLength">左侧定制</param>
        /// <param name="reightCustomizationLength">右侧定制</param>
        /// <param name="minLength">最小区间值</param>
        /// <param name="maxLenth">最大区间值</param>
        /// <param name="leftInline">进水口内嵌</param>
        /// <param name="embeddedContinue">true为内嵌，false为外延</param>
        /// <param name="rightInlineExtension">出水口内嵌外延值</param> 
        public static (List<double> lengthOfPanelSubsection,List<double> SectionSubsection) GetslabCulvertSubsectionData(
            double slabCulvertLength,double leftCustomizationLength,double reightCustomizationLength, 
            double minLength, double maxLenth, double leftInline, bool embeddedContinue,out double rightInlineExtension)
        {
            //板长
            double lengthOfPanel = 1;
            //中间长度
            double centreLength = Convert.ToDouble(Convert.ToDecimal(slabCulvertLength) - Convert.ToDecimal(leftCustomizationLength) - Convert.ToDecimal(reightCustomizationLength) - Convert.ToDecimal(leftInline));
            if (centreLength != (int)centreLength)
            {
                if (embeddedContinue == true)
                {
                    rightInlineExtension = Convert.ToDouble( Convert.ToDecimal(centreLength) - Convert.ToDecimal((int)centreLength));
                    centreLength = (int)centreLength;
                }
                else
                {
                    rightInlineExtension = Convert.ToDouble( Convert.ToDecimal((int)centreLength) + 1 - Convert.ToDecimal(centreLength));
                    centreLength = (int)centreLength + 1;
                }
            }
            else
                rightInlineExtension = 0;
            //板长分段数据
            List<double> lengthOfPanelSubsection = new List<double>();
            lengthOfPanelSubsection.Add(leftCustomizationLength);
            for (int i = 0; i < centreLength; i++)
            {
                lengthOfPanelSubsection.Add(lengthOfPanel);
            }
            lengthOfPanelSubsection.Add(reightCustomizationLength);
            //区间分段数据
            if (leftCustomizationLength < minLength)
            {
                centreLength = Convert.ToDouble(Convert.ToDecimal(centreLength) - (Convert.ToDecimal(leftCustomizationLength) - Convert.ToDecimal((int)leftCustomizationLength) + Convert.ToDecimal(minLength) - Convert.ToDecimal(leftCustomizationLength)));
                leftCustomizationLength = Convert.ToDouble(Convert.ToDecimal(leftCustomizationLength) - Convert.ToDecimal((int)leftCustomizationLength) + Convert.ToDecimal(minLength));
            }
            if (reightCustomizationLength < minLength)
            {
                centreLength = Convert.ToDouble(Convert.ToDecimal(centreLength) - (Convert.ToDecimal(reightCustomizationLength) - Convert.ToDecimal((int)reightCustomizationLength) + Convert.ToDecimal(minLength) - Convert.ToDecimal(reightCustomizationLength)));
                reightCustomizationLength = Convert.ToDouble(Convert.ToDecimal(reightCustomizationLength) - Convert.ToDecimal((int)reightCustomizationLength) + Convert.ToDecimal(minLength));
            }
            List<double> SectionSubsection = new List<double>();
            //SectionSubsection.Add(leftCustomizationLength);
            //if (centreLength >= 2 * minLength)
                SectionSubsection.AddRange(CastInPlaceSlabCulvertSubsection.Calc(lengthOfPanelSubsection, maxLenth, minLength, lengthOfPanel).ToList());
           // else
                //SectionSubsection.Add(centreLength);
            //SectionSubsection.Add(reightCustomizationLength);
            return (lengthOfPanelSubsection, SectionSubsection);
        }
        

        //计算区间内标准值的最佳倍数值
        public static double GetSectionMinimumValue(double standard, double MinLength, double MaxLength)
        {
            double i = 1;
            double value = 0;
            while (true)
            {
                value = standard * i;
                if (value >= MinLength && value <= MaxLength)
                    return value;
                i++;
            }
        }
        //区间分段
        public static double[] Calc(double OverallLength, double MaxLength, double MinLength, double componentLength)
        {
            double[] Segment = null;
            try
            {
                //区间内标准值的最佳倍数值
                MinLength = GetSectionMinimumValue(componentLength, MinLength, MaxLength);

                int num = (int)(OverallLength / MinLength);
                var remainder = OverallLength % MinLength;
                Segment = new double[num];
                for (int i = 0; i < num; i++)
                {
                    Segment[i] = MinLength;
                }

                bool flag = false;

                while (remainder > 0)
                {
                    for (int i = 0; i < num; i++)
                    {
                        var length = Segment.Length - 1 - i;
                        if (Segment[length] >= MaxLength)
                        {
                            flag = true;
                            break;
                        }
                        if (remainder >= componentLength)
                        {
                            Segment[length] += componentLength;
                            remainder -= componentLength;
                        }
                        else
                        {
                            Segment[length] += remainder;
                            remainder -= remainder;
                        }
                        if (remainder == 0)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag == true)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Segment;
        }

    }
}
